package com.yun.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDTO;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.UserContext;
import com.yun.wemedia.mapper.WmMaterialMapper;
import com.yun.wemedia.service.WmMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.heima.model.common.enums.AppHttpCodeEnum.DATA_NOT_EXIST;

/**
 * @author Excell
 * @description 针对表【wm_material(自媒体图文素材信息表)】的数据库操作Service实现
 * @createDate 2025-07-23 08:25:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial>
        implements WmMaterialService {


    private final FileStorageService fileStorageService;

    @Override
    public ResponseResult queryList(WmMaterialDTO wmMaterialDTO) {
        if (wmMaterialDTO == null) {
            return ResponseResult.errorResult(DATA_NOT_EXIST);
        }


        // 构造分页条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<WmMaterial>();

        if (wmMaterialDTO.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, wmMaterialDTO.getIsCollection());
        }

        Long userId = UserContext.getUser();

        queryWrapper.eq(WmMaterial::getUserId, userId);

        // 进行分页查询
        Page<WmMaterial> wmMaterialPage = new Page<>(wmMaterialDTO.getPage(), wmMaterialDTO.getSize());
        Page<WmMaterial> page = this.page(wmMaterialPage, queryWrapper);


        // 获取分页内容
        List<WmMaterial> records = page.getRecords();

        PageResponseResult pageResponseResult =
                new PageResponseResult(
                        wmMaterialDTO.getPage(), (int) page.getSize(), (int) page.getTotal()
                );
        // pageResponseResult.setSize(Integer.parseInt(((Long) page.getSize()).toString()));
        // pageResponseResult.setTotal(Integer.parseInt(((Long) page.getTotal()).toString()));
        // pageResponseResult.setSize((int) page.getSize());
        // pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setData(records);

        return pageResponseResult;
    }




    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Integer.parseInt(UserContext.getUser().toString()) );
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection(0);
        wmMaterial.setType(0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        //4.返回结果

        return ResponseResult.okResult(wmMaterial);
    }
}




